<?php

namespace app\api\controller\v1;

use think\Controller;
use think\Request;
use app\api\controller\v1\Common;
use app\api\validate\Article as ArticleValidate;
use app\api\model\Article as ArticleModel;

class Article extends Common
{
    protected $params;
    public function __construct(Request $request) {
//        parent::initialize();
        $data = $request->except(['time','token']);
        $validate = new ArticleValidate();
        $action = $request->action();
        if (!$validate->scene($action)->check($data)) {
            $this->msg(400,$validate->getError());
        }
        $this->params = $data;
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        /* 接收参数 */
        $data = $this->params;
        $data['num'] = isset($data['num'])?$data['num']:10;
        $data['page'] = isset($data['page'])?$data['page']:1;
        /* 查询数据库 */
        $field = 'article.article_id,article.article_uid,article.article_title,article.create_time,user.user_nickname';
        $list =  ArticleModel::hasWhere('user','',$field)->where('article_uid',$data['user_id'])->limit($data['num'])->order('article_id', 'desc')->paginate($data['num'],'',[
            'page'=>$data['page']
        ]);
        if($list===false){
            $this->msg(400,'查询失败');
        }elseif(empty($list)){
            $this->msg(400,'暂时没有记录');
        }else{
            $this->msg(200,'查询成功',$list);
        }
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function save()
    {
        /* 获取参数 */
        $data = $this->params;
        $data['create_time'] = $data['update_time'] = time();
        /* 添加文章 */
        $article = new ArticleModel();
        $res = $article->allowField(true)->save($data);
        if($res!==false){
            $this->msg(200,'文章添加成功！',$article->id);
        }else{
            $this->msg(400,'文章添加失败！');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read()
    {
        /* 获取参数 */
        $data = $this->params;
        /* 查询数据库 */
        $field = 'article.article_id,article.article_uid,article.article_title,article_content,article.create_time,user.user_nickname';
        $info =  ArticleModel::hasWhere('user','',$field)->where('article_id',$data['article_id'])->order('article_id', 'desc')->find();
        if($info){
            $info['article_content'] = htmlspecialchars_decode($info['article_content']);
            $info['create_time'] = date('Y-m-d H:i:s',$info['create_time']);
            $this->msg(200,'文章查询成功',$info);
        }else{
            $this->msg(400,'查询失败！'.$data['article_id']);
        }
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update()
    {
        /* 接收参数 */
        $data = $this->params;
        /* 更新数据库 */
        $article = new ArticleModel();
        $res = $article->allowField(true)->save($data,['article_id'=>$data['article_id']]);
        if($res !== false){
            $this->msg(200,'文章更新成功');
        }else{
            $this->msg(400,'更新失败！');
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete()
    {
        /* 获取参数 */
        $data = $this->params;
        /* 删除文章 */
        $res = ArticleModel::destroy($data['article_id']);
        if($res!==false){
            $this->msg(200,'文章删除成功!');
        }else{
            $this->msg(400,'文章删除失败！');
        }
    }
}
